//
//  CheckinScreen.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 6/7/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "CheckinScreen.h"
#import "CheckinViewController.h"

#define VIEW_BAR_HEIGHT 25
#define VIEW_CHECKIN_HEIGHT 48+VIEW_BAR_HEIGHT
#define VIEW_MAINCOMMENT_HEIGHT 196+VIEW_CHECKIN_HEIGHT
#define VIEW_TITLE_HEIGHT 25+VIEW_MAINCOMMENT_HEIGHT
#define VIEW_PERSONLIKE_HEIGHT 84+VIEW_TITLE_HEIGHT

#define BTN_CHECKIN_WIDTH 95
#define BTN_CHECKIN_HEIGHT 38

@implementation CheckinScreen
@synthesize parent = _parent;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setContentSize:CGSizeMake(self.bounds.size.width, 500)];
        self.showsVerticalScrollIndicator = NO;
        _vBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, VIEW_BAR_HEIGHT)]; 
        _vBar.backgroundColor = [UIColor blackColor];
        
            // Draw view checkin
        _vCheckin = [[UIView alloc]initWithFrame:CGRectMake(0, VIEW_BAR_HEIGHT, self.bounds.size.width, 48)]; 
        _vCheckin.backgroundColor = [UIColor grayColor];
        
        _btnBackCheckin = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 48)];
        [_btnBackCheckin addTarget:self action:@selector(btnBackCheckinClicked) forControlEvents:UIControlEventTouchUpInside];
        _img_back_checkin = [UIImage imageNamed:IMG_BACK_CHECKIN];
        _btnBackCheckin.backgroundColor = [UIColor colorWithPatternImage:_img_back_checkin];
        [_vCheckin addSubview:_btnBackCheckin];
        
        _btnCheckin = [[UIButton alloc]initWithFrame:CGRectMake(self.bounds.size.width - BTN_CHECKIN_WIDTH -5, 5, BTN_CHECKIN_WIDTH, BTN_CHECKIN_HEIGHT)];
        [_btnCheckin addTarget:self action:@selector(btnLoginClicked) forControlEvents:UIControlEventTouchUpInside];
        _img_checkin = [UIImage imageNamed:IMG_CHECKIN];
        _btnCheckin.backgroundColor = [UIColor colorWithPatternImage:_img_checkin];
        [_vCheckin addSubview:_btnCheckin];
        
            // Draw view main comment
        _vMainComment = [[UIView alloc]initWithFrame:CGRectMake(0, VIEW_CHECKIN_HEIGHT, self.bounds.size.width, 196)];
        _img_main_comment = [UIImage imageNamed:IMG_MAINCOMMENT];
        _vMainComment.backgroundColor = [UIColor colorWithPatternImage:_img_main_comment];
        
        _txtComment = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 242, 176)];
        _txtComment.backgroundColor = [UIColor clearColor];
        _txtComment.delegate = self;
        [_txtComment setFont:[UIFont italicSystemFontOfSize:14]];
        _txtComment.selectedRange = NSMakeRange(0, 140);
        
        placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0, _txtComment.frame.size.width - 20.0, 34.0)];
        [placeholderLabel setText:STRING_COMMENT];
            // placeholderLabel is instance variable retained by view controller
        [placeholderLabel setBackgroundColor:[UIColor clearColor]];
        [placeholderLabel setFont:[UIFont italicSystemFontOfSize:14]];
        [placeholderLabel setTextColor:[UIColor lightGrayColor]];
        
            // Insert view to control text comment
        _vControlText = [[UIView alloc]initWithFrame:CGRectMake(0, 450, self.bounds.size.width, 30)];
        _vControlText.backgroundColor = [UIColor whiteColor];
        _btnDone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _btnDone.frame = CGRectMake(self.bounds.size.width - 50, 5, 40, 20);
        [_btnDone setTitle:BTN_DONE_TITLE forState:UIControlStateNormal];
        [_btnDone addTarget:self action:@selector(btnDoneClicked) forControlEvents:UIControlEventTouchUpInside];
        [_vControlText addSubview:_btnDone];

        [_txtComment addSubview:placeholderLabel];
        [_vMainComment addSubview:_txtComment];
        
            // Draw view title
        _vTitleMonAn = [[UIView alloc]initWithFrame:CGRectMake(0, VIEW_MAINCOMMENT_HEIGHT, self.bounds.size.width, 25)];
        _img_title = [UIImage imageNamed:IMG_TITLE_MONAN];
        _vTitleMonAn.backgroundColor = [UIColor colorWithPatternImage:_img_title];
        
            // Draw view people like
        _personLike = [[UIView alloc]initWithFrame:CGRectMake(0, VIEW_TITLE_HEIGHT, self.bounds.size.width, 84)];
        _img_view_personlike = [UIImage imageNamed:IMG_VIEW_PERSONLIKE];
        _personLike.backgroundColor = [UIColor colorWithPatternImage:_img_view_personlike];
        _personLikeScreeen = [[PersonLikeScreen alloc]initWithFrame:CGRectMake(20, 10, self.bounds.size.width - 40, 65)];
        [_personLike addSubview:_personLikeScreeen];
            // Draw view map
//        _vMap = [UIView alloc]
        
        [self addSubview:_vBar];
        [self addSubview:_vCheckin];
        [self addSubview:_vMainComment];
        [self addSubview:_vTitleMonAn];
        [self addSubview:_personLike];
    }
    return self;
}

#pragma mark - Control txtComment
- (void)textViewDidChange:(UITextView *)textView {
    if(![_txtComment hasText]) {
        [_txtComment addSubview:placeholderLabel];
        [UIView animateWithDuration:0.15 animations:^{
            placeholderLabel.alpha = 1.0;
        }];
    } else if ([[_txtComment subviews] containsObject:placeholderLabel]) {
        [UIView animateWithDuration:0.15 animations:^{
            placeholderLabel.alpha = 0.0;
        } completion:^(BOOL finished) {
            [placeholderLabel removeFromSuperview];
        }];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    if (![_txtComment hasText]) {
        [_txtComment addSubview:placeholderLabel];
        [UIView animateWithDuration:0.15 animations:^{
            placeholderLabel.alpha = 1.0;
        }];
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    [UIView animateWithDuration:0.2 animations:^{
        _vControlText.frame = CGRectMake(0, 260, 320, 30);
    [self addSubview:_vControlText];
    [self setContentOffset:CGPointMake(0, 45)];
    }];
    [self setScrollEnabled:NO];
    return 1;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    [UIView animateWithDuration:0.5 animations:^{
        [_vControlText removeFromSuperview];
        [self setContentOffset:CGPointMake(0, 0)];
    }];
    return 1;
}

- (void) btnDoneClicked {
    [UIView animateWithDuration:0.5 animations:^{
        [_vControlText removeFromSuperview];
        [self setContentOffset:CGPointMake(0, 0)];
    }];
    [_txtComment resignFirstResponder];
    [self setScrollEnabled:YES];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSUInteger oldLength = [_txtComment.text length];
    NSUInteger replacementLength = [text length];
    NSUInteger rangeLength = range.length;
    
    NSUInteger newLength = oldLength - rangeLength + replacementLength;
    
    return newLength <= MAX_COMMENT_LENGTH;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self setScrollEnabled:YES];
    [_txtComment resignFirstResponder];
}

#pragma mark - Control button click
- (void) btnBackCheckinClicked {
    CheckinViewController *checkinViewController = (CheckinViewController *)_parent;
    [checkinViewController switchToFoodProfileScreen];
} 

- (void) btnLoginClicked {
    
}
@end
